﻿
namespace KingerRobot.MexicoSailunWMS.SpiService;
internal class BMesCuringRequestMaterial : IMesCuringRequestMaterial
{
    public List<POMesCuringRequestMaterial> GetCuringRequestMaterialByMaterialId(long materialId)
    {
        var db = Db.Default;
        var result = db.MesCuringRequestMaterials.Where(t => t.MaterialId == materialId).ToList();
        return result;
    }
    public List<CuringRequestMaterialGroup> GetCuringRequestMaterialGroup()
    {
        var db = Db.Default;
        var RequestMaterials = db.MesCuringRequestMaterials.ToList();
        var list = RequestMaterials.GroupBy(t => t.MaterialId).Select(t => new CuringRequestMaterialGroup
        {
            MaterialId = t.Key.Value,
        }).ToList();

        foreach (var item in list)
        {
            var equipNos = RequestMaterials.Where(t => t.MaterialId == item.MaterialId).Select(t => t.EquipNo).Distinct().ToList();
            var str = "";
            foreach (var equipNo in equipNos)
            {
                str += equipNo + "，";
            }
            str = str.Remove(str.Length - 1);
            item.CuringCode = str;
        }

        return list;
    }

    public int AddOrUpdateCuringRequestMaterial(POMesCuringRequestMaterial pOMesCuringRequestMaterial)
    {
        var db = Db.Default;

        var mesCuringRequestMaterial = db.MesCuringRequestMaterials.Where(t => t.CuringLocId == pOMesCuringRequestMaterial.CuringLocId).FirstOrDefault();

        var result = 0;
        if (mesCuringRequestMaterial is null)
        {
            // 添加
            pOMesCuringRequestMaterial.Id = XID.Nextval;
            result = db.MesCuringRequestMaterials.Insert(pOMesCuringRequestMaterial);
        }
        else
        {
            // 修改
            result = db.MesCuringRequestMaterials.Where(t => t.CuringLocId == pOMesCuringRequestMaterial.CuringLocId).Update(pOMesCuringRequestMaterial);
        }

        return result;
    }

    public POMesCuringRequestMaterial GetCuringRequestMaterialByLocId(long locId)
    {
        var db = Db.Default;
        var result = db.MesCuringRequestMaterials.Where(t => t.CuringLocId == locId).FirstOrDefault();
        return result;
    }

    public List<CuringOutOrderBean> GetCuringOutOrderDetails()
    {
        var db = Db.Default;
        var result = db.MesCuringRequestMaterials.GroupJoin(db.WmsOutOrderSummarys, f => f.CuringLocId, s => s.TargetLoc, (f, s) => new
        {
            MesCuringRequestMaterials = f,
            WmsOutOrderSummarys = s,
        }).SelectMany(f => f.WmsOutOrderSummarys.DefaultIfEmpty(), (f, s) => new
        {
            f.MesCuringRequestMaterials,
            WmsOutOrderSummarys = s,
        }).GroupJoin(db.WcsTasks, f => f.WmsOutOrderSummarys.Id, s => s.SummaryId, (f, s) => new
        {
            f.MesCuringRequestMaterials,
            f.WmsOutOrderSummarys,
            WcsTasks = s,
        }).SelectMany(f => f.WcsTasks.DefaultIfEmpty(), (f, s) => new
        {
            f.MesCuringRequestMaterials,
            f.WmsOutOrderSummarys,
            WcsTasks = s,
        }).Select(t => new CuringOutOrderBean
        {

            CuringTotalTime = t.MesCuringRequestMaterials.CuringTotalTime,
            EquipModel = t.MesCuringRequestMaterials.EquipModel == 2 ? "左模" : "右模",
            MaterialCode = t.MesCuringRequestMaterials.MesMaterialId,
            EquipNo = t.MesCuringRequestMaterials.EquipNo,
            MesOrderId = "",
            OrderStatus = t.WmsOutOrderSummarys == null ? "未创建" : "已创建",
            MesCreateTime = t.MesCuringRequestMaterials.CreateTime,
            //MesLatestTime=
            TaskCreateTime = t.WcsTasks.CreateTime,
        }).ToList();



        return result;
    }

}